package com.ims.client.util;

import com.alibaba.fastjson.JSONObject;
import com.ims.client.constant.*;
import oshi.SystemInfo;
import oshi.hardware.*;

import java.util.ArrayList;
import java.util.List;


public class GenerateDeviceInfoUtil {

    public static JSONObject getAllDeviceInfo() {
        JSONObject result = new JSONObject();

        String deviceId = "";
        if ("LINUX".equals(System.getProperty("os.name").toUpperCase())){
            deviceId = GetDeviceIdUtil.getLinuxDeviceId();
        }else{
            deviceId = GetDeviceIdUtil.getWindowsDeviceId();
        }

        String deviceName = DeviceNameUtil.getDeviceName();
        JSONObject cpuInfo = getCpuInfo();
        List<JSONObject> diskInfo = getDiskDriverInfo();
        List<JSONObject> ramInfo = getRamInfo();
        List<JSONObject> networkAdapterInfo = getNetworkAdapterInfo();

        result.put(HardwareInfoField.DEVICE_ID, deviceId);
        result.put(HardwareInfoField.DEVICE_NAME, deviceName);
        result.put(HardwareInfoField.CPU_INFO, cpuInfo);
        result.put(HardwareInfoField.DISK_DRIVER_INFO, diskInfo);
        result.put(HardwareInfoField.RAM_INFO, ramInfo);
        result.put(HardwareInfoField.NETWORK_ADAPTER_INFO, networkAdapterInfo);
        result.put(CommonField.EVENT_TIME, DateUtil.getFullTime());

        return result;
    }

    public static JSONObject getCpuInfo(){

        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();

//        Console.log("======= CPU信息 ======");
        CentralProcessor cpu = hardware.getProcessor();
        CentralProcessor.ProcessorIdentifier cpuInfo = cpu.getProcessorIdentifier();

        JSONObject result = new JSONObject();
        result.put(CpuInfoField.CPU_ID, cpuInfo.getProcessorID());
        result.put(CpuInfoField.CPU_NAME, cpuInfo.getName());
        result.put(CpuInfoField.CPU_IDENTIFIER, cpuInfo.getIdentifier());
        result.put(CpuInfoField.CPU_MODEL, cpuInfo.getModel());
        result.put(CpuInfoField.PHYSICAL_PACKAGE_COUNT, cpu.getPhysicalPackageCount());
        result.put(CpuInfoField.PHYSICAL_PROCESSOR_COUNT, cpu.getPhysicalProcessorCount());
        result.put(CpuInfoField.LOGICAL_PROCESSOR_COUNT, cpu.getLogicalProcessorCount());

//        Console.log(result);

        return result;
    }

    public static List<JSONObject> getDiskDriverInfo(){
        List<JSONObject> result = new ArrayList<>();

        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();

//        Console.log("======= 物理磁盘信息 ======");
        List<HWDiskStore> diskList = hardware.getDiskStores();
        diskList.forEach((disk) -> {
            JSONObject item = new JSONObject();
            item.put(DiskDriverInfoField.DISK_NAME, disk.getName());
            item.put(DiskDriverInfoField.DISK_MODEL, disk.getModel());
            item.put(DiskDriverInfoField.DISK_SERIAL, disk.getSerial());
            item.put(CommonField.TOTAL, FormatSizeUtil.format(disk.getSize()));
            result.add(item);
        });
//        Console.log(result);

        return result;
    }

    public static List<JSONObject> getRamInfo(){
        List<JSONObject> result = new ArrayList<>();

        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();

//        Console.log("======= 内存信息 ======");
        GlobalMemory memory = hardware.getMemory();
        List<PhysicalMemory> memoryList = memory.getPhysicalMemory();
        if (memoryList.size() == 0){
            JSONObject item = new JSONObject();
            item.put(RamInfoField.MEMORY_MODEL, "Virtual Machine RAM");
            item.put(RamInfoField.MEMORY_TYPE, "Virtual Machine RAM");
            item.put(RamInfoField.CLOCK_SPEED, "Virtual Machine RAM");
            item.put(CommonField.TOTAL, FormatSizeUtil.format(memory.getTotal()));
            result.add(item);
        }
        memoryList.forEach((ram) -> {
            JSONObject item = new JSONObject();
            item.put(RamInfoField.MEMORY_MODEL, ram.getManufacturer());
            item.put(RamInfoField.MEMORY_TYPE, ram.getMemoryType());
            item.put(RamInfoField.CLOCK_SPEED, ram.getClockSpeed() / 1000000 + " MHz");
            item.put(CommonField.TOTAL, FormatSizeUtil.format(ram.getCapacity()));
            result.add(item);
        });
//        Console.log(result);

        return result;
    }

    public static List<JSONObject> getNetworkAdapterInfo(){
        List<JSONObject> result = new ArrayList<>();

        SystemInfo systemInfo = new SystemInfo();
        HardwareAbstractionLayer hardware = systemInfo.getHardware();
        List<NetworkIF> adapterList = hardware.getNetworkIFs();
        adapterList.forEach((adapter)->{
            JSONObject item = new JSONObject();
            item.put(NetworkAdapterInfoField.ADAPTER_NAME, adapter.getName());
            item.put(NetworkAdapterInfoField.MAC_ADDR, adapter.getMacaddr());
            item.put(NetworkAdapterInfoField.IPV4, adapter.getIPv4addr());
            item.put(NetworkAdapterInfoField.IPV6, adapter.getIPv6addr());
            result.add(item);
        });

        return result;
    }
}
